// zdl:: now we are required to make serval threads

#include <pthread.h>
#include <iostream>
#include <vector>
#include <cstdio>
#include <unistd.h>

using namespace std;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void *active(void *args)
{

    std::string name = static_cast<char *>(args);
    while (true)
    {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond, &mutex); // zdl:: 现在是没有对资源的判定机制的
        printf("%s is activated!\n", name.c_str());
        pthread_mutex_unlock(&mutex);
    }
    return nullptr;
}
int main()
{
    pthread_t t1, t2, t3;
    // std::vector<pthread_t> threads{t1, t2, t3};
    // for (int i = 1; i <= 3; i++)
    // {
    //     std::string name("thread-" + std::to_string(i));
    //     pthread_create(&threads[i - 1], nullptr, active, const_cast<char *>(name.c_str()));
    // }
    pthread_create(&t1, nullptr, active, const_cast<char *>("thread-1"));
    pthread_create(&t2, nullptr, active, const_cast<char *>("thread-2"));
    pthread_create(&t3, nullptr, active, const_cast<char *>("thread-3"));

    sleep(1);
    printf("main thread ctl begin ....\n");
    while (true)
    {
        printf("Main thread wakeup threads ....\n");
        // pthread_cond_signal(&cond);
        pthread_cond_broadcast(&cond); // 一次唤醒所有的线程
        sleep(1);
    }
    // for (int i = 1; i <= 3; i++)
    pthread_join(t1, nullptr);
    pthread_join(t2, nullptr);
    pthread_join(t3, nullptr);

    return 0;
}

// zdl:: 生产者消费者模型
